﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using Consul;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 反填开票通知单命令处理方法
    /// </summary>
    public class FanTianInvoiceCommandHandler:IRequestHandler<FanTianInvoiceCommand,APIResult<List<FanInvoiceSettlementEntityDtos>>>
    {
        /// <summary>
        /// 开票通知单的仓储接口
        /// </summary>
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;
        private readonly IBaseRepository<OrderMaterials> oRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iRepo"></param>
        public FanTianInvoiceCommandHandler(IBaseRepository<InvoiceSettlementEntity>iRepo,IBaseRepository<OrderMaterials>oRepo)
        {
            this.iRepo = iRepo;
            this.oRepo = oRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<FanInvoiceSettlementEntityDtos>>> Handle(FanTianInvoiceCommand request, CancellationToken cancellationToken)
        {

            var list = from a in iRepo.GetAll().Result
                       join b in oRepo.GetAll().Result on a.DocumentCode equals b.SaleCode
                       select new FanInvoiceSettlementEntityDtos
                       {
                           InvoiceSettlementEntityId  = a.InvoiceSettlementEntityId,
                           DocumentCode = a.DocumentCode,
                           BusinessType = a.BusinessType,
                           CustomerName = a.CustomerName,
                           Bank = a.Bank,
                           DeliveryEmail = a.DeliveryEmail,
                           DocumentDate = a.DocumentDate,
                           Operator = a.Operator,
                           InvoiceTypeFlag = a.InvoiceTypeFlag,
                           AccountNumber = a.AccountNumber,
                           Remark = a.Remark,
                           DocumentStatus = a.DocumentStatus,
                           Auditor = a.Auditor,
                           TaxRate = a.TaxRate,
                           TaxNumber = a.TaxNumber,
                           BusinessStatus = a.BusinessStatus,
                           AuditDate = a.AuditDate,
                           InvoiceType = a.InvoiceType,
                           AddressPhone = a.AddressPhone,
                           SaleCode  = b.SaleCode,
                           MaterialsCode = b.MaterialsCode,
                           MaterialsName = b.MaterialsName,
                           ModelSpecification = b.ModelSpecification,
                           Calulate = b.Calulate,
                           CalulateNumber = b.CalulateNumber,
                           Price = b.Price,
                           TotalPrice = b.TotalPrice,
                           QualityRequir = b.QualityRequir,
                           PackRequir = b.PackRequir
                       };
            if(request.InvoiceSettlementEntityId != null )
            {
                list = list.Where(X => X.InvoiceSettlementEntityId == request.InvoiceSettlementEntityId);
            }

            APIResult<List<FanInvoiceSettlementEntityDtos>> result = new APIResult<List<FanInvoiceSettlementEntityDtos>>();
            result.data = list.ToList();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.Count = list.Count();
            return Task.FromResult(result);



        }



    }


}
